package com.sjsu.aws.ephermeralnode;

import java.util.HashMap;
import org.apache.log4j.Logger;
import com.amazonaws.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodb.model.ComparisonOperator;
import com.amazonaws.services.dynamodb.model.Condition;
import com.amazonaws.services.dynamodb.model.ScanResult;
import com.sjsu.aws.db.QueryCriteria;
import com.sjsu.aws.db.TableOperationImpl;
import com.sjsu.aws.db.TableRow;

/**
 * 
 * @author Pawar Deepali
 * 
 */
public class EphemeralServiceImpl implements EphemeralService {
	
	private final static Logger	LOG	= Logger.getLogger(EphemeralServiceImpl.class);
	
	private TableOperationImpl	        tableOperationImpl;
	private TableRow	                tableRow;
	private QueryCriteria	            criteria;
	
	public EphemeralServiceImpl() throws Exception {
		this.tableOperationImpl = new TableOperationImpl();
		this.tableRow = new TableRow();
		this.criteria = new QueryCriteria();
	}
	
	@Override
	public void deleteEphemeralNodesOnDisconnect(String tableName,
	        String uuIdClmnName, String conditionClmnName) {
		LOG.info("Delete Ephemeral Nodes On Disconnect for " + tableName
		        + "table");
		ScanResult scanResult = tableOperationImpl
		        .queryTableWithNoCondition(tableName);
		String uuid = null;
		for (int i = 0; i < scanResult.getCount(); i++) {
			HashMap<String, AttributeValue> item = (HashMap<String, AttributeValue>) scanResult
			        .getItems().get(i);
			if (item.get(conditionClmnName).getS() != null) {
				uuid = item.get(uuIdClmnName).getS();
				LOG.info("UUID value is " + item.get(uuIdClmnName).getS());
				tableRow.setDeleteKey(uuid);
				tableOperationImpl.deleteItem(tableName, tableRow);
			}
		}
	}
	
	@Override
	public void renewEphemeralMembership(String tableName, String uuIdClmnName,
	        String endTimeStampClmnName, String conditionClmnName,
	        String conditionClmnValue, long timeStamp) throws Exception {
		
		LOG.info("Renew Ephemeral Membership for " + tableName + "table");
		
		HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
		Condition condition = new Condition().withComparisonOperator(
		        ComparisonOperator.EQ.toString()).withAttributeValueList(
		        new AttributeValue().withS(conditionClmnValue));
		
		scanFilter.put(conditionClmnName, condition);
		criteria.setScanFilter(scanFilter);
		
		String uuid = null;
		ScanResult scanResult = tableOperationImpl.queryTable(tableName,
		        criteria);
		
		for (int i = 0; i < scanResult.getCount(); i++) {
			HashMap<String, AttributeValue> item = (HashMap<String, AttributeValue>) scanResult
			        .getItems().get(i);
			if (item.get(conditionClmnName).getS() != null) {
				uuid = item.get(uuIdClmnName).getS();
				LOG.info("UUID value is " + item.get(uuIdClmnName).getS());
				tableRow.setUpdateKey(uuid);
				tableRow.setUpdateColumnName(endTimeStampClmnName);
				tableRow.setUpdateColumnValue(String.valueOf(timeStamp));
				tableOperationImpl.UpdatePutItemWithN(tableName, tableRow);
			}
		}
	}
	
	@Override
	public void deleteTimeOutSessionsForEphemeralNode(String tableName,
	        String uuIdClmnName, String endTimeStampClmnName) throws Exception {
		long currentTime = System.currentTimeMillis();
		
		HashMap<String, Condition> scanFilter = new HashMap<String, Condition>();
		
		Condition condition = new Condition().withComparisonOperator(
		        ComparisonOperator.LT).withAttributeValueList(
		        new AttributeValue().withN(String.valueOf(currentTime)));
		
		scanFilter.put(endTimeStampClmnName, condition);
		
		criteria.setScanFilter(scanFilter);
		
		ScanResult scanResult = tableOperationImpl.queryTable(tableName,
		        criteria);
		
		LOG.info("Current time in millis=" + currentTime);
		LOG.info("Number of Ephemeral nodes to delete after timeout="
		        + scanResult.getCount());
		for (int i = 0; i < scanResult.getCount(); i++) {
			
			HashMap<String, AttributeValue> item = (HashMap<String, AttributeValue>) scanResult
			        .getItems().get(i);
			if (item.get(uuIdClmnName).getS() != null) {
				
				String uuid = item.get(uuIdClmnName).getS();
				LOG.info("UUID value is " + item.get(uuIdClmnName).getS());
				tableRow.setDeleteKey(uuid);
				tableOperationImpl.deleteItem(tableName, tableRow);
				
			}
		}
	}
	
}
